package com.xiaofan.aspect;

import com.xiaofan.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-06-28 20:03
 **/
@Aspect
@Component
public class CustomAspect {
    @Pointcut("execution(* com..service..*(..))")
    public void servicePointCut(){
    }
    @Around("servicePointCut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        Object target = proceedingJoinPoint.getTarget();
        Class<?> aClass = target.getClass();
        Field userMapper = aClass.getDeclaredField("userMapper");
        userMapper.setAccessible(true);
        userMapper.set(target,sqlSession.getMapper(userMapper.getType()));
        return proceedingJoinPoint.proceed();
    }
}
